package com.hnevc.netfoodchoose;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodDetailActivity extends AppCompatActivity {
    TextView tvFoodName;
    TextView tvFoodPrice;
    TextView tvFoodDesc;
    ImageView ivFoodPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        //首先获取传递过来intent ,获取传递通过的参数foodid

        Intent intent = getIntent();
        final int foodId = intent.getIntExtra("foodId", 0);
        //获取控件
        tvFoodName = (TextView) findViewById(R.id.id_tv_foodName);
        tvFoodPrice = (TextView) findViewById(R.id.id_tv_foodPrice);
        tvFoodDesc = (TextView) findViewById(R.id.id_tv_foodDesc);
        ivFoodPic = (ImageView) findViewById(R.id.id_iv_foodPic);

      /*  String urls="http://10.1.63.232:8080/RestaruantServer/Food/ShwFood";*/
        new Thread(new Runnable() {
            public void run() {
                try {
                    //创建url字符串
                    String urls = "http://"
                            + getResources().getString(R.string.ServerIp)
                            + ":8080/RestaurantServer/Food/SHowFood" + foodId;
             /*
                String urls="http://10.1.63.232:8080/RestaruantServer/Food/ShwFood" + foodId;*/
                    //构建访问客户段，支持cookie
                    OkHttpClient client = new OkHttpClient.Builder()
                            .cookieJar(new MyCookieJar())
                            .build();
                    Request request = new Request.Builder()
                            .url(urls)
                            .get()
                            .build();

           /* Response response= null;*/
                    Response response = client.newCall(request).execute();
                /*response = client.newCall(request).execute();*/
                    String data = response.body().string();
                    Gson gson = new Gson();
                    final Food food = gson.fromJson(data, Food.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvFoodName.setText(food.getFoodName());
                            tvFoodPrice.setText(food.getFoodPrice() + "");
                            tvFoodDesc.setText(food.getFoodDesc());
                            //根据当前的food，构建食物图片的路径
                            String foodpicurl = "http://"
                                    + getResources().getString(R.string.ServerIp)
                                    + ":8080/RestaurantServer/uploadFoodImage/"
                                    + food.getFoodPic();
                            //picasso下载并显示图片
                            Picasso.with(FoodDetailActivity.this)//上下文
                                    .load(foodpicurl)//设置图片来源
                                    .into(ivFoodPic);//设置图片在那个控件显示
                        }
                    });


                } catch (
                        IOException e)

                {
                    e.printStackTrace();
                }
            }

        }).start();
    }
}
